package com.example.springevent.listener;

import com.example.springevent.event.AsyncEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 异步事件监听器 - 使用@Async注解异步处理事件
 */
@Slf4j
@Component
public class AsyncEventListener {
    
    @Async
    @EventListener
    public void handleAsyncEvent(AsyncEvent event) {
        log.info("异步处理事件开始: {}", event.getMessage());
        
        try {
            // 模拟耗时操作
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        log.info("异步处理事件完成: {}, 线程: {}", 
                event.getMessage(), 
                Thread.currentThread().getName());
    }
}